<?php
namespace Home\Controller;
use Think\Controller;
class AlipayController extends OrderController {

    private function clearcart($uid){
        // 删除用户购物车数据
        $Cart = M("Cart");
        $Cart->where("buyer_id = $uid")->delete();
    }

    public function price($paysn){
        $order = M('order');
        $data = $order->field('order_amount,pay_sn')->where("pay_sn = '$paysn'")->find();
        if(!$data) returnApiError(1,'订单号不存在');
        return $data['order_amount'];
    }

    /**
     * 支付宝回调通知
     */
    public function notify(){
        Vendor('alipay/alipayapi');
        $AlipayNotify = new \AlipayNotify();
        //$_POST['notify_id'] = '21610b8819bbc87454e113a61cc76f5kv2';
        if(empty($_POST)){
            //logResult('支付宝回调数据为空');
           // returnApiError(1,'支付宝回调数据为空');
        }

        $success = false;
        $result = $AlipayNotify->verifyNotify($_POST);
        if(($_POST['trade_status'] == 'TRADE_SUCCESS')  || ($_POST['trade_status'] == 'TRADE_FINISHED')){
            $success = true;
        }

        if($result && $success){
           // logResult('验签成功');
             //处理订单数据
            $order = M('order');
            $order_pay = M('order_pay');
            $order_sn = $_POST['out_trade_no'];
            $total_fee = $_POST['total_fee'];

            //查询是否重复提交
            $pay_where = array(
                'pay_sn' => $order_sn,
                'api_pay_state' => '1',
            );
            if($order_pay->where($pay_where)->find()){
                echo 'success';die;
            }


            $order_arr = array(
                'payment_time' => time(),
                'order_state' => 20,
                'payment_code' => 'alipay',
            );
            $where['pay_sn'] = $order_sn;
            $total_fee_data = $order->where($where)->find();
            if($total_fee_data['order_amount'] != $total_fee){
               // returnApiError(1,'支付价格和订单价格不匹配');
            }

            $order->where($where)->save($order_arr);

            $pay_arr = array(
                'api_pay_state' => 1,
            );
            $order_pay->where("pay_sn = $order_sn")->save($pay_arr);
            //下单赠积分
            $pointlog = D('PointsLog');  
            $insertarr["pl_memberid"] =$total_fee_data['buyer_id'];
            $insertarr["pl_membername"] =$total_fee_data['buyer_name'];
            $insertarr["order_sn"]= $total_fee_data['order_sn'];
            $insertarr["order_id"]=$total_fee_data["order_id"];
            $insertarr['orderprice']=$total_fee_data['order_amount'];
            $result = $pointlog->insert("order",$insertarr);
            $this->clearcart($total_fee_data['buyer_id']);
            echo 'success';
        }else{
           return false;
        }
    }

    public function return_url(){

    }

    public function index(){
        require_once("./ThinkPHP/Library/Vendor/alipay/alipay.config.php");
        Vendor('alipay/alipayapi');

        $paysn = I('post.order_sn');
        if(!$paysn)  returnApiError(1,'缺少订单sn号');
        $price = $this->price($paysn);
        $param = array(
            'partner' => '"2088811907938392"',
            'seller_id' => '"pengrun@un-eagle.com"',
          //  'out_trade_no' => '"4684684787484"',
            'out_trade_no' => '"'.$paysn.'"',
            'subject' => '"商品详情"',
            'body' => '"商品详情"',
            'total_fee' => '"'.$price.'"',    //总金额
            'notify_url' => '"'.API_URL.'/api/index.php/Home/Alipay/notify.html"',
            'service' => '"mobile.securitypay.pay"',
            'payment_type' => '"1"',
            '_input_charset' => '"utf-8"',
            'it_b_pay' => '"30m"',
            'return_url' => '"'.API_URL.'/api/index.php/Home/Alipay/return_url.html"',
        );

        $json_param = array(
            'partner' => '2088811907938392',
            'seller_id' => 'pengrun@un-eagle.com',
            'out_trade_no' => $paysn,
            'subject' => '商品详情',
            'body' => '商品详情',
            'total_fee' => $price,    //总金额
            'notify_url' => API_URL.'/api/index.php/Home/Alipay/notify.html',
            'service' => 'mobile.securitypay.pay',
            'payment_type' => '1',
            '_input_charset' => 'utf-8',
            'it_b_pay' => '30m',
            'return_url' => API_URL.'/api/index.php/Home/Alipay/return_url.html',
        );
		//$param = argSort(paraFilter($param));
		$prestr = createLinkstring($param);
        $sign = rsaSign($prestr,'./ThinkPHP/Library/Vendor/alipay/key/rsa_private_key.pem');

        $json_param['sign'] = $sign;
        $json_param['sign_type'] = 'RSA';

        returnApiSuccess('ok',$json_param);

    }


    public function ios(){
        require_once("./ThinkPHP/Library/Vendor/alipay/alipay.config.php");
        Vendor('alipay/alipayapi');

        $Order = D('order');
        $paysn = I('post.order_sn');
        if(!$paysn)  returnApiError(1,'缺少订单sn号');
        $price = $this->price($paysn);
        $param = array(
            'partner' => '"2088811907938392"',
            'seller_id' => '"pengrun@un-eagle.com"',
            'out_trade_no' => '"'.$paysn.'"',
            'subject' => '"商品详情"',
            'body' => '"商品详情"',
            'total_fee' => '"'.$price.'"',    //总金额
            'notify_url' => '"'.API_URL.'/api/index.php/Home/Alipay/notify.html"',
            'service' => '"mobile.securitypay.pay"',
            'payment_type' => '"1"',
            '_input_charset' => '"utf-8"',
            'it_b_pay' => '"30m"',
            //'show_url' => '"m.alipay.com"',
        );

        $json_param = array(
            'partner' => '2088811907938392',
            'seller_id' => 'pengrun@un-eagle.com',
            'out_trade_no' => $paysn,
            'subject' => 'test',
            'body' => 'teststst',
            'total_fee' => $price,    //总金额
            'notify_url' => API_URL.'/api/index.php/Home/Alipay/notify.html',
            'service' => 'mobile.securitypay.pay',
            'payment_type' => '1',
            '_input_charset' => 'utf-8',
            'it_b_pay' => '30m',
            //'show_url' => 'm.alipay.com',
        );
        //$param = argSort(paraFilter($param));
        $prestr = createLinkstring($param);
        $sign = rsaSign($prestr,'./ThinkPHP/Library/Vendor/alipay/key/rsa_private_key.pem');

        $json_param['sign'] = $sign;
        $json_param['sign_type'] = 'RSA';
        $json_param['private_key'] = 'MIICXAIBAAKBgQDK/TWLUGKeNpcm4cY+uNw6Z8O1vFL8C2LJE+9Fw8AfUFP/BMc9dMkr0+6elfSztk36nbyOHmTCyTFM1+8ekpQfaz25hTq5lx3/vlS6Hyc548SL3kDJzY6Inc7milL0VqmBo9WwdFcutwrnWyAlxTlFBG6a+kEMghbyB1L0ZqpoGwIDAQABAoGBALvjwMohMAduVF7DIPMxvXe6UGdJ2C5Ag8HH1/RlitHxUq58x8dU8FjHJNpZx1UJbAiMNNn/vBiojh+oyEcuOjds0d1LtVS28nk6dkQpaC10bVV/TZRKT3kCoeQfNQ7RgYwp6v6naAwNfJfTPwl1qjCQaqBlAeOrNQoAtN7jAuTBAkEA5VpkTi4lYtCWl4O6ynp0z2ja1xAWqKflp8uXV3MeSkfFmDg+99BfcjbMz7i0RiK3LNG0yl/S9Jax9JlNU2J0qwJBAOKSrYBd2REaCNHPCWBA4ABRWkKSLW9PkXBMoFRvo+tpq3REyxpWs+Yz3oS5ZzrtK7a7tgDgA9fSjdnK1Ia+elECQE5okaQcqJFYqDMSSNiR9r81pKMjj+uGwr+TnaC67V8NnGLGz9get5KvY2OcTJ0xnxfGdZ0+FzuhDSOHCWV5YlMCQA8EmeuATPsNNrhxeLWglzhuLAVtmwVdyM4gxh/SppLcFjlGtg+w34vKtF0nzOfjAJrEJnQ4ZQjRNAXTo/KqfYECQDra6/+GmfkYPivsOe5hGnZ8nCmRgflBpyGRPwoIJXHbxOrW/mCaH8loLhO/TIAkANcQsNeT72TIddhrmx6Izpg=';
        returnApiSuccess('ok',$json_param);

    }






  

}